import 'package:flutter/material.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/lib_util.dart';

import '../../../../module_common/provider/wallet_info_provider.dart';
import '../../../../module_common/widgets/button/public_button.dart';
import '../../../../module_common/widgets/dialog/bottom_sheet_widget.dart';
import '../../../../module_common/widgets/public_tip/public_tip.dart';
import '../../widget/textFieldWithDelete.dart';

//修改钱包名称弹窗
class WalletEditNameSheet extends StatefulWidget {
  final Wallet? wallet;

  const WalletEditNameSheet({Key? key, required this.wallet}) : super(key: key);

  @override
  _WalletEditNameSheetState createState() => _WalletEditNameSheetState();
}

class _WalletEditNameSheetState extends State<WalletEditNameSheet> {
  late TextEditingController _controller;
  late double _sheetHeight;
  @override
  void initState() {
    super.initState();
    _sheetHeight = 0;
    _controller = TextEditingController();
    _controller.text = context.read<WalletInfoNotifier>().walletName;
  }

  @override
  Widget build(BuildContext context) {
    return BottomSheetWidget(
        title: 'Edit Wallet Name',
        expand: false,
        child: Container(
          padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 20.w),
          child: Column(
            children: [
              Container(
                  height: 50.w,
                  padding: EdgeInsets.only(left: 15.w),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10.w),
                      color: '#F6F6F6'.toColor()),
                  child: TextFieldWithDelete(
                    controller: _controller,
                    hasDelete: true,
                  )),
              SizedBox(height: 20.w),
              PublicButton(
                text: 'Confirm',
                margin: EdgeInsets.zero,
                type: ButtonType.private,
                onClick: () {
                  if (widget.wallet != null && _controller.text.isNotEmpty) {
                    context
                        .read<WalletInfoNotifier>()
                        .updateWalletName(widget.wallet!, _controller.text);
                    // //完成逻辑
                    Toast.show(context,
                            message: 'Operating succeeded',
                            duration: Duration(seconds: 2))
                        .then((_) {
                      Navigator.of(context).pop();
                    });
                  } else {
                    Toast.show(context,
                        message: 'Operation failed',
                        status: '2',
                        duration: Duration(seconds: 2));
                  }
                },
              ),
              KeyboardVisibilityBuilder(builder: (context, isKeyboardVisible) {
                if (isKeyboardVisible) {
                  _sheetHeight = MediaQuery.of(context).viewInsets.bottom + 10;
                } else {
                  _sheetHeight = MediaQuery.of(context).padding.bottom +
                      MediaQuery.of(context).viewInsets.bottom +
                      10;
                }
                return SizedBox(height: _sheetHeight.w);
              })
            ],
          ),
        ));
  }
}
